`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    15:10:09 04/05/2014 
// Design Name: 
// Module Name:    Compass_Indicator 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module Compass_Indicator(
	input clk,
	input reset,
	input  		[2:0] motion_mode,		// 3 bit motion mode as input for the seven possible motion modes
	output reg 	[4:0] compass_pos_dig2, 		// 5 bits indicate the compass position	
	output reg 	[4:0] compass_pos_dig1, 		// 5 bits indicate the compass position	
	output reg 	[4:0] compass_pos_dig0 		// 5 bits indicate the compass position	
);


	//For generating 5Hz frequency clock	
   parameter  simulate = 0;
   localparam rojobot_cnt_5Hz = simulate ? 26'd5			// rojobot clock when simulating
                                      : 26'd19_999_999; // rojobot count when running on HW (5Hz)
	
	localparam rojobot_cnt_10Hz = simulate ? 26'd5			// rojobot clock when simulating
                                      : 26'd9_999_999; // rojobot count when running on HW (5Hz)

	// Rojobot's Motion Modes
	parameter STOP 			= 3'b000;
	parameter TURN_RIGHT_1X = 3'b001;
	parameter TURN_RIGHT_2X = 3'b010;
	parameter TURN_LEFT_1X 	= 3'b011;
	parameter TURN_LEFT_2X 	= 3'b100;
	parameter FORWARD 		= 3'b101;
	parameter REVERSE 		= 3'b110;

	// Outputs
	parameter zero		=	5'd0; 
	parameter one		=	5'd1; 
	parameter two		=	5'd2; 
	parameter three	=	5'd3; 
	parameter four		=	5'd4; 
	parameter five		=	5'd5; 
	parameter six		=	5'd6; 
	parameter seven	=	5'd7; 
	parameter eight	=	5'd8; 	
	parameter nine		=	5'd9; 	

	//Internal variables
	reg [25:0]	ck_count_5Hz = 0;					//clock divider counter
	reg tick5hz;										// 5 Hz clock enable
	reg [25:0]	ck_count_10Hz = 0;				//clock divider counter
	reg tick10hz;										// 10 Hz clock enable
	reg [8:0] count 			= 9'b0;				//9 bit vector for UP/DOWN counter
	reg [4:0] unitsplace 	= 5'b0;
	reg [4:0] tensplace 		= 5'b0;
	reg [4:0] hundredsplace = 5'b0;	
	reg [5:0] quotient		= 5'b0;
	
	//Binary 9 bit UP/DOWN counter
	always @(posedge clk)
		begin
		if(reset)
			count = 9'b0;
		else
		begin	
			if(tick5hz)
			case(motion_mode)
				TURN_RIGHT_1X: 
				if(count == 9'd359)
					count = 9'b0;
				else	
					count = count + 1'b1;
					
				TURN_LEFT_1X:	
				if(count == 9'b0)
					count = 9'd359;
				else	
					count = count - 1'b1;
			endcase

			if(tick10hz)
			case(motion_mode)
				TURN_RIGHT_2X: 
				if(count == 9'd359)
					count = 9'b0;
				else	
					count = count + 1'b1;
				
				TURN_LEFT_2X:		
				if(count == 9'b0)
					count = 9'd359;
				else	
					count = count - 1'b1;
			endcase
		end	
	end

	//Binary to decimal converter
	always @(*)
	begin
		unitsplace	 = count %	4'd10;
		quotient   = count / 4'd10;	
	
		tensplace 	 = quotient % 4'd10;
		quotient   = quotient / 4'd10;	
		
		hundredsplace   = quotient % 4'd10;
	end

	//Lookup table for seven segment display outputs
	always @(*)
	begin
		case(unitsplace)
			zero	: compass_pos_dig0 = zero;
			one 	: compass_pos_dig0 = one;
			two 	: compass_pos_dig0 = two;
			three	: compass_pos_dig0 = three;
			four	: compass_pos_dig0 = four;
			five	: compass_pos_dig0 = five;
			six	: compass_pos_dig0 = six;
			seven	: compass_pos_dig0 = seven;
			eight	: compass_pos_dig0 = eight;
			nine	: compass_pos_dig0 = nine;		
			//default: compass_pos_dig0 = zero;
		endcase
		
		case(tensplace)
			zero	: compass_pos_dig1 = zero;
			one 	: compass_pos_dig1 = one;
			two 	: compass_pos_dig1 = two;
			three	: compass_pos_dig1 = three;
			four	: compass_pos_dig1 = four;
			five	: compass_pos_dig1 = five;
			six	: compass_pos_dig1 = six;
			seven	: compass_pos_dig1 = seven;
			eight	: compass_pos_dig1 = eight;
			nine	: compass_pos_dig1 = nine;		
			
			//default: compass_pos_dig1 = zero;
		endcase
		
		case(hundredsplace)
			zero	: compass_pos_dig2 = zero;
			one 	: compass_pos_dig2 = one;
			two 	: compass_pos_dig2 = two;
			three	: compass_pos_dig2 = three;
			four	: compass_pos_dig2 = four;
			five	: compass_pos_dig2 = five;
			six	: compass_pos_dig2 = six;
			seven	: compass_pos_dig2 = seven;
			eight	: compass_pos_dig2 = eight;
			nine	: compass_pos_dig2 = nine;		

			//default: compass_pos_dig2 = zero;
		endcase
	end

	//5Hz clock is generated here
	always @(posedge clk)
	begin
		if (ck_count_5Hz == rojobot_cnt_5Hz)
		begin
		    tick5hz <= 1'b1;
		    ck_count_5Hz <= 0;
		end
		else
		begin
		    ck_count_5Hz <= ck_count_5Hz + 1'b1;
		    tick5hz <= 0;
      end
	end

	//10Hz clock is generated here
	always @(posedge clk)
	begin
		if (ck_count_10Hz == rojobot_cnt_10Hz)
		begin
		    tick10hz <= 1'b1;
		    ck_count_10Hz <= 0;
		end
		else
		begin
		    ck_count_10Hz <= ck_count_10Hz + 1'b1;
		    tick10hz 	  <= 0;
      end
	end	

endmodule
